package com.jxs.android.ges;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.ges.data.NoticeInfo;
import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.util.MemoryUtil;
import com.jxs.android.util.StartHelper;
import com.jxs.android.weiget.TopBar;
import com.jxs.android.ges.R;

public class LaucherSystemNoticeDetail extends AbstractActivity {

	private LinearLayout loading_ll, notice_content_ll;

	private TextView notice_date;
	private TextView notice_title;
	private WebView notice_content;

	private NoticeInfo noticeInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.notice_detail);

		loading_ll = (LinearLayout) findViewById(R.id.loading_ll);
		notice_content_ll = (LinearLayout) findViewById(R.id.notice_content_ll);

		notice_date = (TextView) findViewById(R.id.notice_date);
		notice_title = (TextView) findViewById(R.id.notice_title);
		notice_content = (WebView) findViewById(R.id.notice_content);
		notice_content.getSettings().setJavaScriptEnabled(true);

		if (null != getIntent() && null != getIntent().getExtras()) {
			noticeInfo = (NoticeInfo) getIntent().getExtras().getSerializable("noticeInfo");
			if (null != noticeInfo) {
				initTextToDisplay(noticeInfo);
				loading_ll.setVisibility(View.VISIBLE);
				notice_content_ll.setVisibility(View.GONE);
				getNoticeDetail(GesApplication.getUserInfo().getTicket(), noticeInfo.getNotice_id());
			}
		}

	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		topBar.hindSpinnerBtn();
		topBarReturn(topBar);
		topBar.setTitle(R.string.notice_title_label);
	}

	private void initTextToDisplay(NoticeInfo noticeInfo) {
		if (null != noticeInfo) {
			notice_title.setText(noticeInfo.getNotice_title());
			notice_date.setText(noticeInfo.getNotice_date());
			String content = noticeInfo.getNotice_content();
			String customHtml = "<html><body style='background:#F5F5F5'>" + content.replaceAll("\n", "<br/>") + "</body></html>";
			// notice_content.loadData(customHtml, "text/html", "UTF-8");
			notice_content.loadDataWithBaseURL(null, customHtml, "text/html", "UTF-8", null);
		}
	}

	private int GET_NEW = 0x0091;
	private int ERROR_MSG = 0x0092;

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (MemoryUtil.isCurrentActivity(LaucherSystemNoticeDetail.class)) {
				if (msg.what == GET_NEW) {
					noticeInfo = (NoticeInfo) msg.obj;
					initTextToDisplay(noticeInfo);
				} else if (msg.what == ERROR_MSG) {
					errorMsgPop(msg.obj.toString());
				}
				loading_ll.setVisibility(View.GONE);
				notice_content_ll.setVisibility(View.VISIBLE);
			}
		}
	};

	private void getNoticeDetail(final String ticket, final String notice_id) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				String wsdl_url = GesApplication.WSDL_URL;// wsdl
				String namespace = GesApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "getNoticeByID";// �???��??��???��
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", ticket);
				request.addProperty("notice_id", notice_id);
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						SoapObject response = (SoapObject) envelope.getResponse();
						paser(response);
					}

					@Override
					public void error(String message, Exception e) {
						Message msg = new Message();
						msg.what = ERROR_MSG;
						msg.obj = message;
						handler.sendMessage(msg);
					}

				});
			}
		}).start();
	}

	private void paser(SoapObject soapObject) {
		if (null != soapObject) {
			String stauts = soapObject.getProperty("status").toString();
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getProperty("message").toString();
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
				StartHelper.startActivity(Login.class);
			} else if ("0".equals(stauts)) {
				SoapObject taskBeanObject = (SoapObject) soapObject.getProperty("retMsg");
				if (null != taskBeanObject) {
					NoticeInfo noticeListInfo = new NoticeInfo();
					noticeListInfo.setNotice_id(taskBeanObject.getProperty("notice_id").toString());
					noticeListInfo.setNotice_title(taskBeanObject.getProperty("notice_title").toString());
					noticeListInfo.setNotice_date(taskBeanObject.getProperty("notice_date").toString());
					noticeListInfo.setNotice_content(taskBeanObject.getProperty("notice_content").toString().replace("\\n", "<br>"));
					Message msg = new Message();
					msg.what = GET_NEW;
					msg.obj = noticeListInfo;
					handler.sendMessage(msg);
				} else {
					Message msg = new Message();
					msg.what = ERROR_MSG;
					msg.obj = message;
					handler.sendMessage(msg);
				}
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
}
